package com.ycy.variable;

import com.ycy.BaseNoListenerTest;
import org.flowable.engine.HistoryService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 这个类通过 taskService 来设置 和 获取 变量
 */
public class VariableDemo1 extends BaseNoListenerTest {

  @Test
  public void query() {
    String procInstId = "4";
    List<Task> list = taskService.createTaskQuery().processInstanceId(procInstId).list();
    System.out.println(list.size());

    for (Task task : list) {
      System.out.println("==>" + task);
      //获取局部变量
      Map<String, Object> variables = taskService.getVariablesLocal(task.getId());
      System.out.println("local variables " + variables);

      Map<String, Object> variables1 = taskService.getVariables(task.getId());
      System.out.println("getVariables " + variables1);

    }

    List<Task> list1 = taskService.createTaskQuery().processInstanceId(procInstId).list();
    for (Task task : list1) {
      System.out.println(task);
    }
  }

  @Test
  public void queryProcessVariable() {
    // 通过Executorid获取变量
    Map<String, Object> variables = runtimeService.getVariables("4");
    System.out.println(variables);
  }

  @Test
  public void addVariables() {
    String procInstId = "4";
    List<Task> list1 = taskService.createTaskQuery().processInstanceId(procInstId).list();
    for (Task task : list1) {
      System.out.println(task);
      // 设置的是流程变量
      Map<String, String> map = new HashMap<>();
      map.put("新增变量", "哈");

      // 局部变量
      Map<String, String> localmap = new HashMap<>();
      localmap.put("新增局部变量", "哈哈哈");
      //局部变量 和 流程变量 同名时，也没关系 因为变量是挂载不同的Executor 下的。
      localmap.put("AA", "哈哈哈");

//      taskService.setVariables(task.getId(), map);
//      taskService.setVariablesLocal(task.getId(), localmap);
//
      runtimeService.setVariables(task.getExecutionId(),map);
      runtimeService.setVariablesLocal(task.getExecutionId(),map);
    }
  }

  @Test
  public void addVariabless() {
    String procInstId = "4";
    List<Task> list1 = taskService.createTaskQuery().processInstanceId(procInstId).list();
    for (Task task : list1) {
      System.out.println(task);
      // 设置的是流程变量
      Map<String, String> map = new HashMap<>();
      map.put("新增变量", "哈");
      taskService.setVariables(task.getId(), map);
//      task.
    }
  }

  @Test
  public void pass() {
    String procInstId = "4";
    List<Task> list1 = taskService.createTaskQuery().processInstanceId(procInstId).list();
    for (Task task : list1) {
      Map<String, Object> map = new HashMap<>();
      map.put("完成时新增变量" + task.getTaskDefinitionKey(), "哈");
      taskService.complete(task.getId(), map);
    }
  }

}
